//
//  SearchProjectsViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/29.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

protocol FetchResult {
    func giveModel(json: JSON)->Void
}
class SearchProjectsViewController: RefreshedTableViewController,UISearchBarDelegate {

    var delegate: FetchResult?
    var path = ReimbursementProjectList
    private let Projects = "Projects"
    private let Search = "Search"
    var selectedModels: [JSON] = []
    let searchBars = UISearchBar()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UINib.init(nibName: "ProjectSearchCell", bundle: Bundle.main), forCellReuseIdentifier: "ProjectSearchCell")
        searchBars.delegate = self
        self.tableView.tableHeaderView = searchBars
        searchBars.placeholder = "请输入项目编号或名称"
        searchBars.frame = CGRect.init(origin: CGPoint.zero, size: CGSize(width: self.view.width, height: 44))
        self.beginRefresh()
        // Do any additional setup after loading the view.
    }
    
    var filteredSource: [JSON]{
        get{
            return self.source.filter({ (json) -> Bool in
                return self.selectedModels.contains(json) == false
            })
        }
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        self.beginRefresh()
        searchBar.resignFirstResponder()
    }
    
    override var names: Array<String>{
        return [Projects,Search]
    }
    
    override func path(for name: String) -> String {
        return path
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return super.param(for: name) + ["searchKeyWord":searchBars.text ?? ""]
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "ProjectSearchCell")!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? ProjectSearchCell {
            cel.model = self.filteredSource[indexPath.row]
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.filteredSource.count
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if delegate != nil {
            delegate?.giveModel(json: self.filteredSource[indexPath.row])
        }
        self.pop()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
